{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "import urllib\n",
    "import urllib3\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一步：获取access_token\n",
    "### client_id 为官网获取的AK， client_secret 为官网获取的SK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bytes'>\n"
     ]
    }
   ],
   "source": [
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=uGac3Ayg5cyV6rWRr6iFSpnC&client_secret=whAOlmjwvDTSzSIHbaaKb4HMdMr3G6gc&'#注意最后有&不要删掉了\n",
    "request = urllib.request.Request(host)\n",
    "request.add_header('Content-Type', 'application/json; charset=UTF-8')\n",
    "response = urllib.request.urlopen(request)\n",
    "content = response.read()\n",
    "if (content):\n",
    "    print(type(content))#<class 'bytes'>\n",
    "content_str=str(content, encoding=\"utf-8\")\n",
    "content_dir = eval(content_str)\n",
    "access_token = content_dir['access_token']\n",
    "# '''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二步调用API进行人流量统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_num?access_token=24.8308bf975e92b92599d3de9e8456e325.2592000.1597557386.282335-21435880\n",
      "{\"person_num\": 45, \"log_id\": 4320024364065497265}\n"
     ]
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_num\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('api.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img} #注意百度给的文档是说可以用show=true输出图片，正确的参数格式应该是“show”:\"true\"\n",
    "params = urllib.parse.urlencode(params).encode(\"utf-8\")  \n",
    "request_url = request_url + '?access_token=' + access_token\n",
    "print(request_url)\n",
    "request = urllib.request.Request(url=request_url, data=params)\n",
    "request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "response = urllib.request.urlopen(request)\n",
    "content = response.read().decode(\"utf-8\")  \n",
    "if content:\n",
    "    print(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
